package sicau.carManager.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author ITcz
 * @Data 2021-02-10 - 11:32
 */

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class MybatisPlusConfig {

    @Autowired
    DataSourceProperties dataSourceProperties;

    /**
     *   mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        return page;
    }

    /**
    *   由于springboot自动配置，扫描mapper失败
     *   这里参照http://mybatis.org/spring/getting-started.html手动整合mybatis
    * */
//    @Bean
//    public DataSource dataSource(){
//        DriverManagerDataSource dataSource = new DriverManagerDataSource();
//        System.out.println("\n\n\n\n\n\n"+dataSourceProperties.getUrl());
//        dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
//        dataSource.setUrl(dataSourceProperties.getUrl());
//        dataSource.setUsername(dataSourceProperties.getUrl());
//        dataSource.setPassword(dataSourceProperties.getPassword());
//        return dataSource;
//    }

//    @Bean
//    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//        sqlSessionFactoryBean.setDataSource(dataSource());
//        return sqlSessionFactoryBean.getObject();
//    }

    /**
    *   通过这个方法获取代理后的mapper
     * */
//    @Bean
//    public AdminMapper getMapper() throws Exception {
//        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
//        return sqlSessionTemplate.getMapper(sicau.carManager.mapper.AdminMapper.class);
//    }
}
